﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MediaDataModel;
using MediaDataModel.Common.Other;
using MediaClient.UserControls;
using System.Drawing.Printing;

namespace MediaClient.Publish
{
    public partial class PrintForm : BaseMainForm
    {
        long mediaPackId = 1;
        ControlCoord ccPoint = new ControlCoord();
        ControlCoord ccDiscInfo = new ControlCoord();
        Label tempLb = null;
        protected bool isMouseDown = false;
        protected Point mouseOffset;

        public PrintForm()
        {
            ccDiscInfo.X = 150;
            ccDiscInfo.Y = 350;
            ccPoint.X = 10;
            ccPoint.Y = 55;
            InitializeComponent();
        }

        public PrintForm(long mediaPackID)
            : this()
        {
            //this.mediaPackId = mediaPackID;
        }

        private void PrintForm_Load(object sender, EventArgs e)
        {
            //this.TransparencyKey = this.BackColor;
            //this.videoInfo1.BackColor = this.BackColor;
            CreateTitle(mediaPackId);
            CreateContent(mediaPackId);
        }

        private void CreateContent(long mediaPackId)
        {
            using (CCTV6Entities db = new CCTV6Entities())
            {
                List<MetaDataRecord> mdrList = db.MetaDataRecords.Where(item => item.MediaPackID == mediaPackId).ToList();
                foreach (MetaDataRecord mdr in mdrList)
                {
                    MetaDataName mdn = db.MetaDataNames.Where(item => item.ID == mdr.MetaDataID).FirstOrDefault();
                    if (mdn != null)
                    {
                        CreateOneRow(mdn, mdr.MetaDataContent);
                    }
                }
            }
        }

        private void CreateOneRow(MetaDataName mdn, string MetaDataContent)
        {
            CheckBox chk = new CheckBox();
            chk.Size = new System.Drawing.Size(90, 20);
            chk.CheckedChanged += new EventHandler(chk_CheckedChanged);
            chk.Text = mdn.Name + "：";
            chk.Font = new System.Drawing.Font("微软雅黑", 8, FontStyle.Bold);
            chk.Location = new Point(ccPoint.X, ccPoint.Y);
            pnlRight.Controls.Add(chk);
            switch (mdn.DataType)
            {

                case 2:
                    TextBox txt = new TextBox();
                    txt.ScrollBars = ScrollBars.Vertical;
                    txt.Size = new System.Drawing.Size(160, 120);
                    txt.ReadOnly = true;
                    txt.BorderStyle = BorderStyle.None;
                    txt.BackColor = Color.White;
                    txt.Text = MetaDataContent;
                    txt.Multiline = true;
                    txt.Font = new System.Drawing.Font("微软雅黑", 8, FontStyle.Bold);
                    txt.WordWrap = true;
                    txt.Location = new Point(ccPoint.X + 100, ccPoint.Y);
                    pnlRight.Controls.Add(txt);
                    ccPoint.Y += txt.Height + 10;
                    break;
                default:
                    Label lb = new Label();
                    lb.Text = MetaDataContent;
                    lb.Font = new System.Drawing.Font("微软雅黑", 8, FontStyle.Bold);
                    lb.Location = new Point(ccPoint.X + 100, ccPoint.Y);
                    int LblNum = MetaDataContent.Length;
                    int RowNum = 10;
                    float FontWidth = lb.Width / lb.Text.Length;
                    int RowHeight = 15;
                    int ColNum = (LblNum - (LblNum / RowNum) * RowNum) == 0 ? (LblNum / RowNum) : (LblNum / RowNum) + 1;
                    lb.AutoSize = false;
                    lb.Width = (int)(FontWidth * 55.0);
                    lb.Height = RowHeight * ColNum;
                    pnlRight.Controls.Add(lb);
                    ccPoint.Y += lb.Height + 10;
                    break;
            }

        }

        void chk_CheckedChanged(object sender, EventArgs e)
        {

            CCTV6Entities db = new CCTV6Entities();
            //string str = "";
            CheckBox chk = sender as CheckBox;
            //str += chk.Text;
            string tempStr = chk.Text.Split('：')[0];
            MetaDataName mdn = db.MetaDataNames.Where(item => item.Name == tempStr).FirstOrDefault();
            MetaDataRecord mdr = null;
            if (mdn != null)
            {
                mdr = db.MetaDataRecords.Where(item => item.MediaPackID == mediaPackId && item.MetaDataID == mdn.ID).FirstOrDefault();
                //str += mdr.MetaDataContent;
            }

            if (chk.Checked == true)
            {
                if (pnlDisc.Controls.Find("vi" + mdn.ID, true).Count() == 0)
                {
                    VideoInfo vi = new VideoInfo();
                    vi.Text = mdn.Name + "：" + mdr.MetaDataContent;
                    vi.Name = "vi" + mdn.ID;
                    //vi.ContextMenuStrip = cmsLable;
                    vi.Location = new Point(200, 350);
                    vi.Enter += new EventHandler(vi_Enter);
                    vi.DoubleClick += new EventHandler(vi_Leave);
                    pnlDisc.Controls.Add(vi);
                }
                else
                {
                    MessageBox.Show("已有添加相应信息", "消息提示");
                }
            }
            else
            {
                foreach (Control item in pnlDisc.Controls)
                {
                    if (item is Label || item is VideoInfo)
                    {
                        if (item.Name == "vi" + mdn.ID)
                        {
                            pnlDisc.Controls.Remove(item);
                        }
                    }
                }
            }
        }

        void vi_Leave(object sender, EventArgs e)
        {
            VideoInfo vi = sender as VideoInfo;
            Label lb = new Label();
            lb.Location = vi.Location;
            lb.ContextMenuStrip = cmsLable;
            lb.Size = vi.Size;
            lb.AutoSize = false;
            lb.Name = vi.Name;
            lb.BackColor = Color.Transparent;
            lb.DoubleClick += new EventHandler(lb_DoubleClick);
            lb.MouseDown += new MouseEventHandler(lb_MouseDown);
            lb.MouseMove += new MouseEventHandler(lb_MouseMove);
            lb.MouseUp += new MouseEventHandler(lb_MouseUp);
            lb.Text = vi.Text;
            pnlDisc.Controls.Remove(vi);
            pnlDisc.Controls.Add(lb);

        }

        void vi_Enter(object sender, EventArgs e)
        {
            (sender as VideoInfo).BorderStyle = BorderStyle.FixedSingle;
        }

        void lb_DoubleClick(object sender, EventArgs e)
        {
            Label lb = sender as Label;
            VideoInfo vi = new VideoInfo();
            vi.Text = lb.Text;
            vi.Name = lb.Name;
            vi.Size = lb.Size;
            //vi.ContextMenuStrip = cmsLable;
            vi.Location = lb.Location;
            vi.Enter += new EventHandler(vi_Enter);
            vi.DoubleClick += new EventHandler(vi_Leave);
            pnlDisc.Controls.Remove(lb);
            pnlDisc.Controls.Add(vi);
        }

        void lb_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)//按下的是鼠标左键
            {
                isMouseDown = false;//停止移动
            }
            else if (e.Button == MouseButtons.Right)
            {
                tempLb = sender as Label;
            }
        }

        void lb_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown == true)
            {
                int left = (sender as Label).Left + e.X - mouseOffset.X;
                int top = (sender as Label).Top + e.Y - mouseOffset.Y;
                (sender as Label).Location = new Point(left, top);
            }
        }

        void lb_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOffset.X = e.X;
                mouseOffset.Y = e.Y;
                isMouseDown = true;//开始移动
            }
        }

        private void CreateTitle(long mediaPackId)
        {
            Label lb = new Label();
            lb.AutoSize = false;
            lb.Location = new Point(724, 38);
            lb.Name = "lb_" + mediaPackId;
            lb.Dock = DockStyle.Top;
            lb.DoubleClick += new EventHandler(lbTitle_DoubleClick);
            lb.BackColor = Color.FromArgb(212, 226, 232);
            lb.Size = new Size(276, 40);
            using (CCTV6Entities db = new CCTV6Entities())
            {
                MediaPack mp = db.MediaPacks.Where(item => item.ID == mediaPackId).FirstOrDefault();
                if (mp != null)
                {
                    lb.Text = "  " + mp.Name;
                    lb.Font = new System.Drawing.Font("微软雅黑", 9, FontStyle.Bold);
                    lb.TextAlign = ContentAlignment.MiddleLeft;
                    pnlRight.Controls.Add(lb);
                }
            }
        }

        void lbTitle_DoubleClick(object sender, EventArgs e)
        {
            Label lb = new Label();
            lb.Name = (sender as Label).Name + "_show";
            lb.BackColor = Color.Transparent;
            lb.Text = (sender as Label).Text;
            lb.Font = new System.Drawing.Font("微软雅黑", 8, FontStyle.Bold);
            lb.AutoSize = true;
            lb.Location = new Point(200, 350);
            lb.ContextMenuStrip = cmsLable;
            lb.DoubleClick += new EventHandler(lb_DoubleClick);
            lb.MouseDown += new MouseEventHandler(lb_MouseDown);
            lb.MouseMove += new MouseEventHandler(lb_MouseMove);
            lb.MouseUp += new MouseEventHandler(lb_MouseUp);
            pnlDisc.Controls.Add(lb);
        }

        private void btnPrint_MouseDown(object sender, MouseEventArgs e)
        {
            btnPrint.BackgroundImage = global::MediaClient.Properties.Resources.打印_1;
        }

        private void btnPrint_MouseUp(object sender, MouseEventArgs e)
        {
            btnPrint.BackgroundImage = global::MediaClient.Properties.Resources.打印;
        }

        private void btnAddAnnotation_MouseDown(object sender, MouseEventArgs e)
        {
            btnAddAnnotation.BackgroundImage = global::MediaClient.Properties.Resources.增加注释_1;
        }

        private void btnAddAnnotation_MouseUp(object sender, MouseEventArgs e)
        {
            btnAddAnnotation.BackgroundImage = global::MediaClient.Properties.Resources.增加注释;
        }

        private void btnAddAnnotation_Click(object sender, EventArgs e)
        {
            string txtRemark = txtAnnotation.Text.Trim();
            if (!string.IsNullOrEmpty(txtRemark))
            {
                Label lb = new Label();
                lb.AutoSize = true;
                lb.Text = txtRemark;
                lb.ContextMenuStrip = cmsLable;
                lb.BackColor = Color.Transparent;
                lb.Location = new Point(150, 150);
                lb.DoubleClick += new EventHandler(lb_DoubleClick);
                lb.MouseDown += new MouseEventHandler(lb_MouseDown);
                lb.MouseMove += new MouseEventHandler(lb_MouseMove);
                lb.MouseUp += new MouseEventHandler(lb_MouseUp);
                pnlDisc.Controls.Add(lb);
            }
            else
            {
                MessageBox.Show("注释不能为空！", "消息提示");
            }
        }

        private void tsmiDelete_Click(object sender, EventArgs e)
        {
            pnlDisc.Controls.Remove(tempLb);
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
            printPreviewDialog1.Document = printDocument1;
            printDocument1.OriginAtMargins = true;
            Margins mg = new Margins(int.Parse(txtX.Text.Trim()), 0, int.Parse(Y.Text.Trim()), 0);
            printDocument1.DefaultPageSettings.Margins = mg;
            if (printPreviewDialog1.ShowDialog() == DialogResult.OK)
            {
            }
        }

        void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            Bitmap sourceBitmap = new Bitmap(pnlDisc.BackgroundImage);

            foreach (Control c in pnlDisc.Controls)
            {
                if (c is Label)
                {
                    c.DrawToBitmap(sourceBitmap, new Rectangle(c.Location, c.Size));//31+c.Location.X,16+c.Location.Y,c.Size.Width,c.Size.Height
                }
            }
            g.DrawImage(sourceBitmap, new Rectangle(int.Parse(txtX.Text.Trim()), int.Parse(Y.Text.Trim()), 464, 464));
            barcodeControl1.Draw(g, new Rectangle(31 + barcodeControl1.Location.X, 16 + barcodeControl1.Location.Y, barcodeControl1.Size.Width, barcodeControl1.Size.Height), GraphicsUnit.Inch, 0.01f, 0, null);

        }


    }
}
